package fm.dongman.modellib.source;

import org.jetbrains.annotations.NotNull;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;

import fm.dongman.animefm.contractlib.IModel;
import fm.dongman.animefm.contractlib.contract.*;
import fm.dongman.modellib.RemoteHelper;
import fm.dongman.modellib.StaticArea.StaticUrl;
import fm.dongman.modellib.model.MsgModel;
import fm.dongman.modellib.remote.RemoteOperationHelper;

/**
 * Created by KUIGE on 2017/11/22.
 */

public class ChatEditRepository implements ChatContract.IChatDataSource {

    private static ChatEditRepository SINGLETON;

    public static ChatEditRepository getInstance() {
        if (SINGLETON == null) {
            SINGLETON = new ChatEditRepository();
        }
        return SINGLETON;
    }

    private ChatEditRepository() {

    }

    @Override
    public void sendChat(@NotNull String userId, @NotNull IModel.IMsgModel chat, @NotNull LoadSourceCallback<? super IModel.IMsgModel> callback) {
        MsgModel model = (MsgModel) chat;
        if (!model.getContent().equals("")) {
            sendContent(callback, model);
            return;
        }
        obtainChatId(callback, model);
    }

    /**
     * 获取私信 id
     *
     * @param callback 回调
     * @param model    模型
     */
    private void obtainChatId(@NotNull LoadSourceCallback<? super IModel.IMsgModel> callback, MsgModel model) {
        HashMap<String, String> map1 = new HashMap<>();
        map1.put("x-token", StaticUrl.TOKEN_KEY);
        HashMap<String, String> map2 = new HashMap<>();
        map2.put("receive_user_id", model.getUserId());
        JSONObject jSONObject = RemoteHelper.Companion.getInstance().parsePostUrl(StaticUrl.GET_CHAT_ID, map1, map2);
        if (jSONObject == null) {
            callback.onDataLoadFiled();
            return;
        }
        try {
            model.setChartId(jSONObject.getJSONObject("data").optString("chart_id"));
            callback.onDataLoaded(model);
        } catch (JSONException e) {
            callback.onDataLoadFiled();
            e.printStackTrace();
        }
    }

    /**
     * 发送内容
     *
     * @param callback 回调
     * @param model    消息
     */
    private void sendContent(@NotNull LoadSourceCallback<? super IModel.IMsgModel> callback, MsgModel model) {
        HashMap<String, String> map = new HashMap<>();
        map.put("receive_user_id", model.getUserId());
        map.put("content", model.getContent());
        map.put("session_key", StaticUrl.TOKEN_KEY);
        JSONObject jSONObject = RemoteHelper.Companion.getInstance().parsePostUrl(StaticUrl.SEND_MSG, map);
        if (jSONObject == null) {
            callback.onDataLoadFiled();
            return;
        }
        try {
            model.setChartId(jSONObject.getJSONObject("data").getString("chart_id"));
            List<IModel.IMsgModel> models = obtainMsgInfo(model.getChartId());
            if (models == null) {
                callback.onDataLoadFiled();
                return;
            }
            model = (MsgModel) models.get(models.size() - 1);
            callback.onDataLoaded(model);
        } catch (JSONException e) {
            callback.onDataLoadFiled();
            e.printStackTrace();
        }
    }

    private List<IModel.IMsgModel> obtainMsgInfo(String chatId) {
        HashMap<String, String> map = new HashMap<>();
        map.put("chart_id", chatId);
        map.put("page", "1");
        map.put("size", "100000");
        map.put("session_key", StaticUrl.TOKEN_KEY);
        JSONObject jSONObject = RemoteHelper.Companion.getInstance().parsePostUrl(StaticUrl.MSG_INFO, map);
        if (jSONObject == null) {
            return null;
        }

        return RemoteOperationHelper.getOperationHelper().downCharModel(jSONObject);
    }

    @Override
    public void setChatId(String chatId, LoadSourceCallback<? super List<? extends IModel.IMsgModel>> loadSourceCallback) {

        List<IModel.IMsgModel> model = obtainMsgInfo(chatId);
        if (model != null) {
            loadSourceCallback.onDataLoaded(model);
        } else {
            loadSourceCallback.onDataLoadFiled();
        }

    }

    @Override
    public void getData(@NotNull LoadSourceCallback<? super List<? extends IModel.IMsgModel>> callback) {
    }

    @Override
    public void saveData(List<? extends IModel.IMsgModel> dataModel) {

    }

    @Override
    public void refreshData() {

    }

    @Override
    public void offLine() {
    }
}
